package com.badlogic.androidgames.mypirates.Sprite;

import java.util.Queue;
import java.util.LinkedList;
import android.graphics.Point;
import android.graphics.Rect;
import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.Graphics;



public class Ship extends MySprite {

	public enum ShipState {
		Standby, Turning, Moving, Attacking, WhirlIn, WhirlOut, Ended };
		
	public enum MainSailDirections { North(0), East(4), South(8), West(12);
		private int value;
		private MainSailDirections(int value){
			this.value = value;
		}
	};
	
	public Point LastPosition;
	public boolean IsTurningClockwise;
    public int turnStep = 4;
    public int currentStep = 1;
    public int currentDirection = 4;
    public int newX;
    public int newY;
    
	public ShipState currentShipState;
	public MainSailDirections mainSailDirection;
	
	protected Queue<Rect> movingAnimation = new LinkedList<Rect>();
	
	public boolean IsActive;
	
	@Override
	public Rect source() {
		Rect temp = frames.get(currentFrame);
		int x = temp.left;
		int x1 = x + temp.width() * currentDirection;
		return new Rect(x1, temp.top, x1 + temp.width(), temp.top + temp.height());
	}
	
	public Ship(Game game) {
		super(game);
		mainSailDirection = MainSailDirections.East;
		currentShipState = ShipState.Standby;
	}
	
	
	@Override
	public void Update(float gameTime) {
		super.Update(gameTime);
		if(currentShipState == ShipState.Turning){
			if(currentStep <= turnStep){
				if(IsTurningClockwise)
					currentDirection = (currentDirection + 1) % 16;
				else
					if(currentDirection == 0)
						currentDirection = 15;
					else
						currentDirection--;
				
				currentStep++;
			}
			else{
				currentShipState = ShipState.Moving;
				x = newX;
				y = newY;
			}
		}
		else if(currentShipState == ShipState.Moving){
			if(movingAnimation.size() == 0){
				currentShipState = ShipState.Standby;
				IsActive = false;
			}
		}
	}
	
	@Override
	public void Draw(Graphics g) {
		if(currentShipState == ShipState.Moving){
			g.drawBitmap(Texture,source(), movingAnimation.poll());
		}
		else if(currentShipState == ShipState.Standby
				|| currentShipState == ShipState.Turning
				|| currentShipState == ShipState.Attacking){
			super.Draw(g);
		}
	}
	
	public void Move(int _newX, int _newY)
    {
		if(x == _newX && y == _newY)
			return;
        LastPosition = new Point(x, y);
        int newDirection = -1;
        if (x == _newX)
        {
            if (_newY > y)
            {
                if (mainSailDirection != MainSailDirections.South)
                {
                    newDirection = MainSailDirections.South.value;
                    turnStep = newDirection - currentDirection;
                    mainSailDirection = MainSailDirections.South;
                }
                Rect currentDest = destination();

                for (int i = 1; i < 21; i++)
                {
                	int x = currentDest.left;
                	int y = currentDest.top + i * 2;
                    movingAnimation.add(new Rect(x, y, x + currentDest.width(), y + currentDest.height()));
                }

            }
            else
            {
                if (mainSailDirection != MainSailDirections.North)
                {
                    newDirection = MainSailDirections.North.value;
                    turnStep = newDirection - currentDirection;
                    mainSailDirection = MainSailDirections.North;
                }

                Rect currentDest = destination();

                for (int i = 1; i < 21; i++)
                {
                    int x = currentDest.left;
                	int y = currentDest.top - i * 2;
                    movingAnimation.add(new Rect(x, y, x + currentDest.width(), y + currentDest.height()));
                }

            }
        }
        else if (y == _newY)
        {
            if (_newX > x)
            {
                if (mainSailDirection != MainSailDirections.East)
                {
                    newDirection = MainSailDirections.East.value;
                    turnStep = newDirection - currentDirection;
                    mainSailDirection = MainSailDirections.East;
                }

                Rect currentDest = destination();

                for (int i = 1; i < 21; i++)
                {
                    int x = currentDest.left + i * 2;
                	int y = currentDest.top;
                    movingAnimation.add(new Rect(x, y, x + currentDest.width(), y + currentDest.height()));
                }

            }
            else
            {
                if (mainSailDirection != MainSailDirections.West)
                {
                    newDirection = MainSailDirections.West.value;
                    turnStep = newDirection - currentDirection;
                    mainSailDirection = MainSailDirections.West;
                }

                Rect currentDest = destination();

                for (int i = 1; i < 21; i++)
                {
                	int x = currentDest.left - i * 2;
                	int y = currentDest.top;
                    movingAnimation.add(new Rect(x, y, x + currentDest.width(), y + currentDest.height()));
                }

            }
        }
        //Set dieu kien de xoay va chieu` xoay
        if (turnStep > 0)
        {
            if (turnStep == 12)
            {
                IsTurningClockwise = false;
                turnStep = 4;
            }
            else
                IsTurningClockwise = true;
        }
        else
        {
            if (turnStep == -12)
            {
                IsTurningClockwise = true;
                turnStep = 4;
            }
            else
            {
                IsTurningClockwise = false;
                turnStep *= -1;
            }
        }
        if (newDirection != -1)
        {
            currentShipState = ShipState.Turning;
            currentStep = 1;

        }
        else
        {
            currentShipState = ShipState.Moving;
            x = _newX;
            y = _newY;
        }

        newX = _newX;
        newY = _newY;
    }
}
